/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fourbox.server;

import fourbox.Metafile;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hartono
 */
public class ServerDownloader implements Runnable {

    List queue;

    public static void main(String args[]) throws IOException {
        File repoDir = new File("Repository/tono");

        List queue = Collections.synchronizedList(new ArrayList());

        synchronized (queue) {
            long last = 1336001270720l;
            File fileA = new File("a.txt");
            Metafile a = Metafile.createMetafile(fileA, last);
            a.isFile = true;
            System.out.println("a " + a);

            queue.add(a);

            File fileB = new File("a/b/c");
            Metafile b = Metafile.createMetafile(fileB, 1335976635701l);
            System.out.println("b " + b);
            queue.add(b);

//            queue.add(Metafile.createMetafile(new File("D:/tes.txt")));
//            queue.add(Metafile.createMetafile(new File("D:/a.pdf")));
//                queue.add(Metafile.createMetafile(new File("D:/a/b/c")));        

//            Metafile metafile  = null;
//            System.out.println("queue size "+queue.size());
//            metafile = (Metafile)queue.remove(0);        
//            System.out.println("Metafile "+metafile);

        }


        ServerDownloader serverDownloader = new ServerDownloader(queue, "tono");
        Thread serverDownloaderThread = new Thread(serverDownloader);
        serverDownloaderThread.start();
    }
    String username;
     public boolean listening;

    public ServerDownloader(List queue, String username) {
        this.queue = queue;
        this.username = username;
    }
    public ServerSocket serverSocket;

    @Override
    public void run() {
        // initialize socket for listening        
        System.out.println("Server Downloader : Starting ...");
        serverSocket = null;
        listening = true;
        try {
            serverSocket = new ServerSocket(Server.SERVER_DOWNLOAD_PORT);
            System.out.println("Server Downloader : Start Listening ...");
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Server Downloader : Cannot listen on port " + Server.SERVER_DOWNLOAD_PORT);
//            System.exit(-1);
        }

        // start listening
        while (listening) {
            try {                
                    new Thread(new ServerDownloadHandler(serverSocket.accept(), queue, username)).start();                
            } catch (IOException ex) {
//                Logger.getLogger(ServerDownloader.class.getName()).log(Level.SEVERE, null, ex);                
                listening = false;
            }
        }
        try {
            serverSocket.close();
        } catch (IOException ex) {
//            Logger.getLogger(ServerDownloader.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Server Downloader : Closing ...");
    }
}
